package org.langqiao.thread;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WelcomeJFrame extends JFrame{
	public WelcomeJFrame(String[] texts)//构造方法
	{
		super("滚动字");
		this.setBounds(300,400,300,400);//设置窗口框架的位置和大小
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭窗口
		if(texts==null||texts.length==0)
			this.getContentPane().add(new RollyJPanel("welcome!"));
		else
		{
			this.getContentPane().setLayout(new GridLayout(texts.length,1));//设置为若干行，1列
			for(int i=0;i<texts.length;i++)
			{
			     this.getContentPane().add(new RollyJPanel(texts[i]));
			}
			
		}
		this.setVisible(true);
	}
	public WelcomeJFrame()
	{
		this(null);
	}
	private class RollyJPanel extends JPanel implements ActionListener,Runnable//内部面板类
	{
		JTextField text_word,text_sleep,text_state;//滚动字的文本行，睡眠的文本行,线程装态的文本行
		JButton button_start,button_interrupt;//启动和中断按钮
		int sleeptime;//定义线程休眠时间
		Thread thread_Rolly;//定义一个线程
	    RollyJPanel(String text)//内部面板类的构造方法
		{
			this.setLayout(new GridLayout(2,1));
			char space[]=new char[100];
			java.util.Arrays.fill(space,' ');//用空格填充space字符数组
			text_word=new JTextField(text+new String(space));//text后加空格字符
			this.add(text_word);//在面板上增加text_word文本行
			
			
			
			JPanel panel_sub=new JPanel(new FlowLayout(FlowLayout.LEFT));
			this.add(panel_sub);
			panel_sub.add(new JLabel("睡眠"));
			this.sleeptime=(int)(Math.random()*100);//产生随机数作为间隔时间
			text_sleep=new JTextField(""+sleeptime,5);//睡眠时间的文本行大小为4
			panel_sub.add(text_sleep);
			text_sleep.addActionListener(this);//睡眠时间文本行注册事件监听器
			
			JButton button_start=new JButton("启动");
			panel_sub.add(button_start);//添加启动按钮
			button_start.addActionListener(this);
			
			JButton button_interrupt=new JButton("中断");
			panel_sub.add(button_interrupt);
			button_interrupt.addActionListener(this);
			
			thread_Rolly=new Thread(this);//创建线程对象，目标对象是当前对象
			button_interrupt.setEnabled(false);//设置中断按钮为无效状态
			panel_sub.add(new JLabel("状态"));
			text_state=new JTextField(""+thread_Rolly.getState(),10);
			text_state.setEditable(false);//状态文本行不可编辑
			panel_sub.add(text_state);			
		}
	    public void run()//线程体，必须用公有方法
	    {
	    	while(true)//线程活动且为中断
	    	{
	    	     try
	    	     {
	    	    	 String str=text_word.getText();
	    	    	 str=str.substring(1)+str.substring(0,1);
	    	    	 text_word.setText(str);
	    	    	 thread_Rolly.sleep(sleeptime);//线程睡眠，抛出中断异常
	    	     }
	    	     catch(InterruptedException e)
	    	     {
	    	    	 break;//退出循环
	    	     }
	    	 }//run()方法结束，线程对象终止
	    }
	    public void actionPerformed(ActionEvent e)//单击事件处理，必须为公有方法
	    {
	    	if(e.getSource()==button_start)
	    	{
	    		thread_Rolly=new Thread(this);//创建一个线程对象
	    		thread_Rolly.start();
	    		text_state.setText("线程状态"+getState());
	    		button_start.setEnabled(false);
	    		button_interrupt.setEnabled(true);
	    	}
	    	if(e.getSource()==button_interrupt)
	    	{
	    		thread_Rolly.interrupt();
	    		text_state.setText("线程状态"+getState());
	    		button_start.setEnabled(true);
	    		button_interrupt.setEnabled(false);
	    	}
	    	if(e.getSource()==text_sleep)
	    	try
	    	{
	    		sleeptime=Integer.parseInt(text_sleep.getText());
	    	}
	    	catch(NumberFormatException nfe)
	    	{
	    		JOptionPane.showMessageDialog(this,"\""+text_sleep.getText()+"\"不能转换成 整数，请重新输入!");
	    	}
	    }
	}
	public static void main(String[] args)
	{
		String Texts[]={"Welcome","Hello","Rolly"};
		new WelcomeJFrame();
	}
		
}
